﻿@{
    ViewBag.Title = "GoodAmountSub";
}

@section scripts
{
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>
    <link href="~/Scripts/miniui/themes/default/medium-mode.css" rel="stylesheet" type="text/css" />      
}
<div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
    <table width = "1000px;">
        <tr>
            <td width = "1000px;">
                <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="NewBill" plain="true">新增</a>
                <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="save" plain="true">保存</a>
                <span class="separator"></span>
                <a class="mini-button" id="modify" name="modify" iconCls="icon-edit" onclick="Edit" plain="true">改单</a>
                <a class="mini-button" id="check" name="check" iconCls="icon-ok" onclick="check" plain="true">审核</a>
                <span class="separator"></span>
                <a class="mini-button" id="close" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
            </td>
        </tr>
    </table>
</div>

<form id="form1" method="post" style="height: 100%">
        <div id = "DivHeader">
                <table style="margin-top:0px;">
                    <tr>
                        <td width="80px" height ="25px" align="right">
                            方案编码：</td>
                        <td width="180px" height ="25px">
                            <input name="FNumber" id="FNumber" class="mini-textbox" style="width:180px;" value="" TabIndex="1" maxLength="20" readonly="true"/>
                        </td>
                        <td width="80px" height ="25px" align="right">
                            促销人群：</td>
                        <td>
                            <input id="combo1" class="mini-combobox" style="width:80px;" textField="text" valueField="id"
                               data="CustType" value="0"  allowInput="false" showNullItem="false"  tabindex="3"/>  
                        </td>
                         <td width="80px" height ="25px" align="right">
                            开始日期：</td>
                        <td width="120px" height ="25px">
                            <input class="mini-datepicker" name="FStartDate" format="yyyy-MM-dd" style="width:120px;" value = "" tabindex="5"/>
                        </td>
                         <td width="80px" height ="25px" align="right">
                            开始时间：</td>
                        <td width="100px" height ="25px">
                            <input id="FStartTime" name="FStartTime" class="mini-timespinner"  format="HH:mm"  style="width:100px;" tabindex="6"/>
                        </td>
                  </tr>
                  <tr>
                        <td width="80px" height ="25px" align="right">
                            方案名称：</td>
                        <td width="180px" height ="25px">
                            <input id="FName" name="FName" class="mini-textbox " style="width:180px;" TabIndex="2"  maxLength="50" value="满额返现"/>
                        </td>
                        <td width="80px" height ="25px" align="right">
                            商品范围：</td>
                        <td width="80px" height ="25px">
                            <input id="combo2" class="mini-combobox" style="width:80px;" textField="text" valueField="id" tabindex="4"
                                data="Gtype" value="3"  allowInput="false" onvaluechanged="onComboxChanged()"/>  
                        </td>
                        <td width="80px" height ="25px" align="right">
                            结束日期：</td>
                        <td width="120px" height ="25px">
                            <input class="mini-datepicker" name="FEndDate" format="yyyy-MM-dd" style="width:120px;" value = "" tabindex="7"/>
                        </td>
                        <td width="80px" height ="25px" align="right">
                            结束时间：</td>
                        <td width="100px" height ="25px">
                            <input id="FEndTime" name="FEndTime" class="mini-timespinner"  format="HH:mm"  style="width:100px;" value="23:59" tabindex="8"/>
                        </td>
                   </tr>
                    <tr>
                        <td width="80px" height ="25px" align="right">
                            星期选择：</td>
                        <td width="200px" height ="35px" colspan="5">
                            <div id="week1" name="week1"  class="mini-checkbox" readOnly="false" text="星期一" style="margin-right:7px;"></div>
                            <div id="week2" name="week2"  class="mini-checkbox" readOnly="false" text="星期二" style="margin-right:7px;"></div>
                            <div id="week3" name="week3"  class="mini-checkbox" readOnly="false" text="星期三" style="margin-right:7px;"></div>
                            <div id="week4" name="week4"  class="mini-checkbox" readOnly="false" text="星期四" style="margin-right:7px;"></div>
                            <div id="week5" name="week5"  class="mini-checkbox" readOnly="false" text="星期五" style="margin-right:7px;"></div>
                            <div id="week6" name="week6"  class="mini-checkbox" readOnly="false" text="星期六" style="margin-right:7px;"></div>
                            <div id="week0" name="week0"  class="mini-checkbox" readOnly="false" text="星期日" ></div>
                       </td>
                        <td colspan="2">
                            <div id="ckonline" name="ckonline" checked="true" class="mini-checkbox" readOnly="false" text="线上" style="margin-right:20px;"></div>
                            <div id="ckline" name="ckline" checked="true"  class="mini-checkbox" readOnly="false" text="线下" style="margin-right:20px;"></div>
                            <div id="ckFObjectID" name="ckFObjectID" checked="true"  class="mini-checkbox" readOnly="false" text="倍数累加"></div>
                        </td>
                    </tr>
                </table>
        </div>
    <div class="mini-fit" >
        <div id="tabs1" class="mini-tabs" activeIndex="0"  style="width:100%; height:100%; top: 0px; left: 0px;overflow:hidden;" plain="false">
            <div id="SelectGood" name="SelectGood" title="商品设置" style="height:100%;overflow:hidden;margin-left:20px; margin-top:5px;">
                <div style="width:100%;">
                    <div class="mini-toolbar" style="border-bottom:0;padding:0px;">
                        <table style="width:100%;">
                            <tr>
                                <td style="width:200px;">
                                    <a name="AddGood" class="mini-button" iconCls="icon-add" onclick="addnew()">新增</a>       
                                    <a name="DelGood" class="mini-button" iconCls="icon-remove" onclick="remove()">删除</a>       
                                </td>
                                <td style="width:200px;">
                                    <input id="setCount" name="setCount" class="mini-spinner" emptyText="请输入满金额" minValue="0" maxValue="99999999" changeOnMousewheel ="false" 
                                        style="width:100px;"  showbutton="false" value="1" increment ="1" selectOnFocus="true" onkeypress="setCount()" tabindex="9"/>   
                                    <a class="mini-button" name="BatchSetCount" onclick="setCount()">批量满金额</a>
                                </td>
                                <td style="width:200px;">
                                    <input id="setDiscount" name="setDiscount" class="mini-spinner" emptyText="请输入返现金额" minValue="0" maxValue="99999999" changeOnMousewheel ="false" 
                                        style="width:100px;"  showbutton="false" value="1" increment ="1" selectOnFocus="true" onkeypress="setPrice()" tabindex="10"/>   
                                    <a class="mini-button" name="BatchSet" onclick="setPrice()">批量返现</a>
                                </td>
                                <td></td>
                            </tr>
                        </table>           
                    </div>
                </div>
                <div id="datagrid1" class="mini-datagrid" style="width:100%;height:88%;" 
                    showPager = "false" showPageSize="false" showPageIndex="false" skipReadOnlyCell="true"
                    allowCellSelect="true" allowSortColumn = "false" allowCellEdit="true" navEditMode="true"
                    editNextOnEnterKey="true"  editNextRowCell="false" oncellendedit="Discount"
                    fitColumns="false">
                    <div property="columns"> 
                    </div>
                </div>
            </div>
            <div id="Storage" name="Storage" title="促销分店" style="height:100%;overflow:hidden;margin-left:20px; margin-top:5px;">
                <table style="width:100%;">
                    <tr>
                        <td width = "200px;">
                            <a class="mini-button" name="AddStock" iconCls="icon-addnew" onclick="getStockItem('')" plain="true">新增</a>
                            <a class="mini-button" name="DelStock" iconCls="icon-remove" onclick="DelStock" plain="true">删行</a>
                        </td>
                        <td>
                            <div id="stockall" name="stockall" checked="true"  class="mini-checkbox" readOnly="false" text="所有分店" onvaluechanged="onstocked()"></div>
                        </td>
                    </tr>
                </table>  
                <div id="datagrid3" class="mini-datagrid" style="width:100%;height:88%;" 
                    showPager = "false" showPageSize="false" showPageIndex="false" skipReadOnlyCell="true"
                    allowCellSelect="true" allowSortColumn = "false" allowCellEdit="true" navEditMode="true"
                    editNextOnEnterKey="true"  editNextRowCell="false"   multiSelect="true"
                    fitColumns="false">
                    <div property="columns">
                        <div type="checkcolumn" ></div>          
                        <div type="indexcolumn" width="40" headerAlign="center" >序号</div>
                        <div field="FNumber" name="FNumber" width="150" headerAlign="center"> 分店编码</div>  
                        <div field="FName" name="FNumber" width="300" headerAlign="center">分店名称</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <input class="mini-hidden" id="step" name="step" value="0"/>
    <input class="mini-hidden" id="FItemID" name="FItemID" value="@ViewBag.FItemID"/>
</form>

<script>
    //var Gtype = [{ "id": "0", "text": "全部" },
    //    { "id": "1", "text": "分类" },
    //    { "id": "2", "text": "品牌" },
    //    { "id": "3", "text": "商品" }];
    var Gtype = [{ "id": "0", "text": "全部" },
    { "id": "2", "text": "品牌" },
    { "id": "3", "text": "商品" }];

    var CustType = [{ "id": "0", "text": "全部" },
    { "id": "1", "text": "非会员" },
    { "id": "2", "text": "会员" }];
    var _Data = new Array(4);
    var _type = 0;
    var datatype = getQueryString("datatype");
    $(function () {
        mini.parse();
        var nowDate = new Date();
        mini.getbyName("FStartDate").setValue(nowDate);
        nowDate.setDate(nowDate.getDate() + 1);
        mini.getbyName("FEndDate").setValue(nowDate);
        createGrid(3);
        modify();
        mini.getbyName("week0").setChecked(true);
        mini.getbyName("week1").setChecked(true);
        mini.getbyName("week2").setChecked(true);
        mini.getbyName("week3").setChecked(true);
        mini.getbyName("week4").setChecked(true);
        mini.getbyName("week5").setChecked(true);
        mini.getbyName("week6").setChecked(true);
        mini.getbyName("AddStock").setEnabled(false);
        mini.getbyName("DelStock").setEnabled(false);
    });
    function FormShown() { }
    function NewBill() {
        mini.getbyName("FItemID").setValue("0");
        mini.getbyName("step").setValue("0");
        mini.getbyName("FNumber").setValue("");
        mini.getbyName("FName").setValue("满额返现");
        mini.get("datagrid1").setData(null);
        mini.get("datagrid3").setData(null);
        SetItemsdisable(true);
        BillState(4);
        mini.get("stockall").setChecked(true);
        mini.getbyName("AddStock").setEnabled(false);
        mini.getbyName("DelStock").setEnabled(false);
        mini.getbyName("FStartTime").setValue('00:00');
        mini.getbyName("FEndTime").setValue('23:59');
        var nowDate = new Date();
        mini.getbyName("FStartDate").setValue(nowDate);
        nowDate.setDate(nowDate.getDate() + 1);
        mini.getbyName("FEndDate").setValue(nowDate);
        //$.post('NewNumber', { mSystemAppID: parent.window.getSystemAppID() }, function (data) {
        //    mini.getbyName("FNumber").setValue(data);
        //});
    }

    function SetFormControl(form, state) {
        var controls = form.getFields();
        for (var i = 0; i < controls.length; i++) {
            if (controls[i] != undefined) {
                if (state == false) {
                    controls[i].disable();
                }
                else {
                    controls[i].enable();
                }
            }
        }
    }
    function SetItemsdisable(state) {
        var form = new mini.Form("form1");
        SetFormControl(form, state);
        mini.get("datagrid1").setAllowCellEdit(state);
        mini.get("datagrid3").setAllowCellEdit(state);
        mini.getbyName("AddStock").setEnabled(state);
        mini.getbyName("DelStock").setEnabled(state);
        mini.getbyName("AddGood").setEnabled(state);
        mini.getbyName("DelGood").setEnabled(state);
        mini.getbyName("BatchSet").setEnabled(state);
        mini.getbyName("BatchSetCount").setEnabled(state);
        mini.getbyName("stockall").setEnabled(state);
    }
    function check() {
        mini.confirm("确定要审核该方案吗？", "提示",
            function (action) {
                if (action == "ok") {
                    checkBill();
                }
            });
    }
    function checkBill() {
            var jsonstr = '[{"FItemID":"' + mini.getbyName("FItemID").getValue() + '","mSystemAppID":"' + parent.window.getSystemAppID() + '"}]';
            $.post("/coupon/PromotionCheck", { json: jsonstr }, function (data) {
                var ret = mini.decode(data);
                if (ret.status == 1) {
                    mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                    BillState(2);
                }
                else {
                    mini.alert("审核失败:" + ret.error);
                }
            });
    }

    function onstocked() {
        if (mini.getbyName("stockall").getChecked()) {
            mini.get("datagrid3").setData(null);
            mini.getbyName("AddStock").setEnabled(false);
            mini.getbyName("DelStock").setEnabled(false);
        }
        else {
            mini.getbyName("AddStock").setEnabled(true);
            mini.getbyName("DelStock").setEnabled(true);
        }
    }

    function save() {
        if (!validata()) {
            mini.alert(Error);
            return;
        }
        var Scheme = GetSchemeJson();
        var ProData = GetProDataJson();
        var objJson = mini.decode(ProData);
        if (mini.get("combo2").getValue() == "1" && objJson.length == 0) {
            mini.alert(" 未选择分类");
            return;
        } else
            if (mini.get("combo2").getValue() == "2" && objJson.length == 0) {
                mini.alert(" 未选择品牌");
                return;
            } else
                if (mini.get("combo2").getValue() == "3" && objJson.length == 0) {
                    mini.alert(" 未选择商品");
                    return;
                }

        var jsonstr = '{"Scheme":\'' + Scheme + '\',"ProData":\'' + ProData + '\'}';
        $.post("savePromotion", { json: jsonstr, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                message = mini.decode(ret.msg);
                mini.getbyName("FItemID").setValue(message[0].FItemID);
                mini.getbyName("FNumber").setValue(message[0].Number);
                SetItemsdisable(false);
                BillState(1);
                mini.showTips({ content: message[0].msg, state: "success", x: "center", y: "top", timeout: 3000 });
            }
            else {
                BillState(4);
                mini.alert("保存失败:" + ret.error);
            }
        });
    }
    function validata() {
        if (mini.getbyName("FName").getValue() == "") {
            Error = "方案名称未输入！";
            return false;
        }
        if (mini.getbyName("FStartDate").getValue() == "") {
            Error = "开始日期未输入！";
            return false;
        }
        if (mini.getbyName("FEndDate").getValue() == "") {
            Error = "结束日期未输入！";
            return false;
        }
        if ((!mini.getbyName("ckonline").getChecked()) && (!mini.getbyName("ckline").getChecked())) {
            Error = "线上、线下至少要选一个";
            return false;
        }
        if ((mini.get("combo2").getValue()+'') == "") {
            Error = "促销范围未选择";
            return false;
        }
        else if (mini.get("combo2").getValue() > 0 && mini.get("datagrid1").getData().length == 0) {
            Error = "未选择" + mini.get("combo2").getText();
            return false;
        }
        if ((mini.get("combo1").getValue()+'') == '') {
            Error = "促销人群未选择";
            return false;
        }
        var t1 = mini.getbyName("FStartDate");
        var t2 = mini.getbyName("FEndDate");
        var startTime = mini.get("FStartTime").getFormattedValue();
        var endTime = mini.get("FEndTime").getFormattedValue();
        var startDate = t1.getFormValue() + ' ' + startTime;
        var endDate = t2.getFormValue() + ' ' + endTime;
        var SDate = new Date(startDate);
        var EDate = new Date(endDate);
        var NDate = new Date();
        if (SDate > EDate) {
            Error = "开始日期不能大于结束日期";
            return false;
        }
        if (NDate > EDate) {
            Error = "结束日期不能小于今日";
            return false;
        }
        return true;
    }
    function GetSchemeJson() {
        var t1 = mini.getbyName("FStartDate");
        var t2 = mini.getbyName("FEndDate");
        var startTime = mini.get("FStartTime").getFormattedValue();
        var endTime = mini.get("FEndTime").getFormattedValue();
        var startDate = (t1.getValue().getFullYear()) + '-' + (t1.getValue().getMonth() + 1) + '-' + t1.getValue().getDate() + ' ' + startTime;
        var endDate = (t2.getValue().getFullYear()) + '-' + (t2.getValue().getMonth() + 1) + '-' + t2.getValue().getDate() + ' ' + endTime;
        var online = 0;
        var line = 0;
        var mObjectID = 0;

        var week = "";
        if (mini.getbyName("week1").getChecked())
            week = "1";
        if (mini.getbyName("week2").getChecked())
            week = week == '' ? "2" : (week + ",2");
        if (mini.getbyName("week3").getChecked())
            week = week == '' ? "3" : (week + ",3");
        if (mini.getbyName("week4").getChecked())
            week = week == '' ? "4" : (week + ",4");
        if (mini.getbyName("week5").getChecked())
            week = week == '' ? "5" : (week + ",5");
        if (mini.getbyName("week6").getChecked())
            week = week == '' ? "6" : (week + ",6");
        if (mini.getbyName("week0").getChecked())
            week = week == '' ? "0" : (week + ",0");
        if (mini.getbyName("ckonline").getChecked()) online = 1;
        if (mini.getbyName("ckline").getChecked()) line = 1;
        if (mini.getbyName("ckFObjectID").getChecked()) mObjectID = 1;

        var json = '{"FItemID":"' + mini.getbyName("FItemID").getValue() + '","FNumber":"' + mini.getbyName("FNumber").getValue() + '","FName":"'
            + mini.getbyName("FName").getValue() + '","FStartDate":"' + startDate + '","FTypeName":"满额返现","FType":"4","setDiscount":' + mini.getbyName("setDiscount").getValue()
            + ',"FEndDate":"' + endDate + '","FStartTime":"' + startTime + '","FEndTime":"' + endTime + '","FWeekDays":"' + week
            + '","FStockIDs":"' + GetStorageJson() + '","FRangeID":"' + mini.get("combo2").getValue()
            + '","FMemberClass":"' + mini.get("combo1").getValue() + '","FSendGoodQty":"0"' + ',"FObjectID":"' + mObjectID + '"'
            + ',"FSendCouponQty":"0","FOnLine":"' + online + '","FLine":"' + line + '","FDataType":"' + datatype + '"}';
        return json;
    }
    function GetStorageJson() {
        var json = '';
        $.each(mini.get("datagrid3").getData(), function (x, e) {
            if (e.FItemID != undefined) {
                if (json == '')
                    json = json + e.FItemID;
                else
                    json = json + ',' + e.FItemID;
            }
        });
        return json;
    }

    function GetProDataJson() {
        var json = '[';
        $.each(mini.get("datagrid1").getData(), function (x, e) {
            if (json != '[')
                json += ',';
            if (mini.get("combo2").getValue() == 0) {
                json = json + '{"FItemID":' + e.FItemID + ',"FRangeID":"0","FRangeExt":0,"FJudgeID":0,"FMin":' + e.FCount
                    + ',"FMax":' + e.FCount + ',"FValue":"' + e.FDiscount + '","FMallValue":"' + e.FDiscount + '"}';
            }
            else if (e.FItemID != undefined) {
                json = json + '{"FItemID":' + e.FItemID + ',"FRangeID":"' + mini.get("combo2").getValue() + '","FRangeExt":'
                    + e.FID + ',"FJudgeID":0,"FMin":' + e.FCount + ',"FMax":' + e.FCount + ',"FValue":"' + e.FDiscount + '","FMallValue":"' + e.FDiscount + '"}';
            }
        });
        json = json + ']';
        if (json == '[]') {
            json = '[{"FItemID":0,"FRangeID":"' + mini.get("combo2").getValue() + '","FRangeExt":0,"FJudgeID":0,"FMin":"0","FMax":"0","FValue":' + mini.getbyName("setDiscount").getValue()
                + ',"FMallValue":' + mini.getbyName("setDiscount").getValue() + '}]';
        }
        return json;
    }

    function BillState(flag) {
        var grid = mini.get("datagrid1");
        var gridstock = mini.get("datagrid3");
        switch (flag) {
            case 0:   //新增
                mini.get("check").setEnabled(false);
                mini.get("save").setEnabled(true);
                mini.get("modify").setEnabled(false);
                grid.setAllowCellEdit(true);
                gridstock.setAllowCellEdit(true);
                break;
            case 1:   //保存
                mini.get("check").setEnabled(true);
                mini.get("save").setEnabled(false);
                mini.get("modify").setEnabled(true);
                grid.setAllowCellEdit(false);
                gridstock.setAllowCellEdit(false);
                break;
            case 2:   //审核
                mini.get("check").setEnabled(false);
                mini.get("save").setEnabled(false);
                mini.get("modify").setEnabled(false);
                grid.setAllowCellEdit(false);
                gridstock.setAllowCellEdit(false);
                break;
            case 3:   //关帐
                mini.get("check").setEnabled(false);
                mini.get("save").setEnabled(false);
                mini.get("modify").setEnabled(false);
                grid.setAllowCellEdit(false);
                gridstock.setAllowCellEdit(false);
                break;
            case 4:   //修改后            
                mini.get("check").setEnabled(false);
                mini.get("save").setEnabled(true);
                mini.get("modify").setEnabled(false);
                grid.setAllowCellEdit(true);
                gridstock.setAllowCellEdit(true);
                break;
        }
    }
    function Edit() {
        BillState(4);
        SetItemsdisable(true);
        onstocked();
        mini.getbyName("FNumber").setEnabled(false);
    }

    function modify() {
        SetItemsdisable(true);
        BillState(4);
        var AppID = parent.window.getSystemAppID();
        if (parseInt(mini.getbyName("FItemID").getValue()) > 0) {
            $.post("getPromotionInfo", { json: '{"FItemID":"' + mini.getbyName("FItemID").getValue() + '","mSystemAppID":"' + AppID + '"}' }, function (data) {
                var form = new mini.Form("form1");
                var mdata = mini.decode(data);
                var Scheme = mini.decode(mdata.Scheme[0]);
                form.setData(Scheme);
                var weekArray = Scheme.FWeekDays.split(',');
                $.each(weekArray, function (index, e) {
                    if (e == '')
                        return true;
                    switch (e) {
                        case '0': mini.getbyName("week0").setChecked(true); break;
                        case '1': mini.getbyName("week1").setChecked(true); break;
                        case '2': mini.getbyName("week2").setChecked(true); break;
                        case '3': mini.getbyName("week3").setChecked(true); break;
                        case '4': mini.getbyName("week4").setChecked(true); break;
                        case '5': mini.getbyName("week5").setChecked(true); break;
                        case '6': mini.getbyName("week6").setChecked(true); break;
                    }
                });
                if (Scheme.FOnLine) mini.getbyName("ckonline").setChecked(true);
                if (Scheme.FLine) mini.getbyName("ckline").setChecked(true);
                if (Scheme.FObjectID == 1) {
                    mini.getbyName("ckFObjectID").setChecked(true);
                }
                else {
                    mini.getbyName("ckFObjectID").setChecked(false);
                }
                mini.getbyName("setCount").setValue(1);
                mini.getbyName("setDiscount").setValue(1);
                mini.get("combo1").setValue(Scheme.FBuyerID);
                mini.get("combo2").setValue(Scheme.FRangeID);
                _type = Scheme.FRangeID;
                mini.get("FStartTime").setValue(Scheme.FStartTime);
                mini.get("FEndTime").setValue(Scheme.FEndTime);
                mini.getbyName("step").setValue("0");
                mini.get("combo2").setValue(Scheme.FRangeID);
                createGrid(mini.get("combo2").getValue());
                if (mdata.Stock.length == 0)
                    mini.getbyName("stockall").setChecked(true);
                else
                    setGrid(mdata.Stock, "datagrid3", 0);
                setGrid(mdata.Good, "datagrid1", Scheme.FRangeID);
                _Data[_type] = mini.get("datagrid1").getData();
                if (Scheme.FChecked || Scheme.FState == -1) {
                    BillState(2);
                    SetItemsdisable(false);
                }
                else {
                    BillState(1);
                    SetItemsdisable(false);
                }
            });
        }
        else {
            //$.post("NewNumber", { mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            //    mini.getbyName("FNumber").setValue(data);
                _type = mini.get("combo2").getValue();
            //});
        }
    }
    function setGrid(action, gridname, rangeid) {
        createGrid(rangeid);
        if (action) {
            var grid = mini.get(gridname);
            for (var i = 0, l = action.length; i < l; i++) {
                var rw = action[i];
                var newRow;
                if (gridname == "datagrid1") {
                    if (rangeid == 3)
                        newRow = {
                            FItemID: rw.FItemID, FID: rw.GoodID, FNumber: rw.GoodNumber, FName: rw.GoodName, FSpec: rw.FSpec, FUnitName: rw.FUnitName, FDiscount: rw.FValue,
                            FPrice: rw.FPosPrice, FPosProPrice: rw.FPosPrice * rw.FValue, FMallPrice: rw.FMallPrice, FMallProPrice: rw.FMallPrice * rw.FValue,
                            FCount: rw.FMin, FCode: rw.FCode/*, FRemark: rw.FRemark*/
                        };
                    else
                        newRow = {
                            FItemID: rw.FItemID, FID: rw.ID, FNumber: rw.Number, FName: rw.Name, FCount: rw.FMin, FDiscount: rw.FValue/*,FRemark: rw.FRemark*/
                        };
                }
                else
                    newRow = { FItemID: rw.FItemID, FNumber: rw.FNumber, FName: rw.FName };
                if (!ExistsInGrid(rw.FItemID, gridname)) {
                    grid.addRow(newRow, grid.getData().length);
                }
            }
        }
    }
    function ExistsInGrid(FItemID, gridName) {
        var grid = mini.get(gridName);
        var ret = false;
        $.each(grid.getData(), function (x, e) {
            if (e.FItemID == FItemID) {
                ret = true;
                return false;
            }
        });
        return ret;
    }

    function ShowGoodsWin(txtValue) {
        mini.open({
            url: "../Bill/GetBillGoods",
            title: "选择商品", width: 900, height: 500,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { key: txtValue, mBillType: "0" };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action == "close") return;
                if (action) {
                    var grid = mini.get("datagrid1");
                    var row = grid.getSelected();
                    if (action.length == undefined) return;
                    for (var i = 0, l = action.length; i < l; i++) {
                        var rw = action[i];
                        var newRow = {
                            FItemID: 0, FID: rw.FItemID, FNumber: rw.FNumber, FName: rw.FName, FSpec: rw.FSpec,
                            FUnitName: rw.FUnitName, FPrice: rw.FPosPrice, FPosProPrice: rw.FPosPrice, FCode: rw.FCode,
                            FMallPrice: rw.FPrice, FMallProPrice: rw.FPrice, FCount: 0, FDiscount: 0
                        };
                        grid.addRow(newRow, grid.getData().length);
                    }
                }
            }
        });
    }
    function ValueChanged(e) {
        if (mini.get("combo2").getValue() == 3)
            ShowGoods(e.value);
        else if (mini.get("combo2").getValue() == 1)
            getGoodsClassItem(e.value);
        else if (mini.get("combo2").getValue() == 2)
            getBrandItem(e.value);
    }

    function remove() {
        var grid = mini.get("datagrid1");
        var row = grid.getSelected();
        if (row) {
            grid.removeRow(row);
        }
    }
    function addnew() {
        var grid = mini.get("datagrid1");
        if (mini.get("combo2").getValue() == 3)
            ShowGoodsWin("");
        else if (mini.get("combo2").getValue() == 1)
            getGoodsClassItem("");
        else if (mini.get("combo2").getValue() == 2)
            getBrandItem("");
        else if (mini.get("combo2").getValue() == 0 && grid.getData().length == 0) {
            var newRow = { FItemID: 0 };
            grid.addRow(newRow, grid.getData().length);
        }
    }
    function setPrice() {
        var grid = mini.get("datagrid1");
        var rows = grid.getData();
        for (var i = 0, l = rows.length; i < l; i++) {
            var row = rows[i]
                grid.updateRow(row, {
                    FDiscount: mini.getbyName("setDiscount").getValue()
                })
        }
    }
    function setCount() {
        var grid = mini.get("datagrid1");
        var rows = grid.getData();
        for (var i = 0, l = rows.length; i < l; i++) {
            var row = rows[i]
            grid.updateRow(row, {
                FCount: mini.getbyName("setCount").getValue()
            })
        }
    }

    function Discount() {
        var grid = mini.get("datagrid1");
        var rows = grid.getData();
        for (var i = 0, l = rows.length; i < l; i++) {
            var row = rows[i]
            if (row.FNumber != undefined) {
                grid.updateRow(row, {
                    FPosProPrice: row.FDiscount,
                    FMallProPrice: row.FDiscount
                })
            }
        }
    }

    function getStockItem(mSearchKey) {
        mini.open({
            url: "../GetItem/multindex",
            title: "门店选择", width: 730, height: 460,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", sTbName: "StockInfo", sSearchKey: mSearchKey };
                iframe.contentWindow.SetData(data);//打开GetItemID.js的SetData
            },
            ondestroy: function (action) {
                if (action) {
                    var grid = mini.get("datagrid3");
                    for (var i = 0, l = action.length; i < l; i++) {
                        grid.addRow({ FItemID: action[i].FItemID, FNumber: action[i].FNumber, FName: action[i].FName }, mini.get("datagrid3").getData().length);
                    }
                }
            }
        });
    }
    function DelStock() {
        mini.confirm("确定要删除选择的记录吗？", "提示",
            function (action) {
                if (action == "ok") {
                    var grid = mini.get("datagrid3");
                    var rows = grid.getSelecteds();
                    $.each(rows, function (index, e) {
                        grid.removeRow(e);
                    });
                }
            });
    }
    function onComboxChanged() {
        var grid = mini.get("datagrid1");
        _Data[_type] = grid.getData();
        createGrid(mini.get("combo2").getValue());
        grid.setData(_Data[mini.get("combo2").getValue()]);
        _type = mini.get("combo2").getValue();
    }
    function createGrid(type) {
        var grid = mini.get("datagrid1");
        if (type == 0) {
            grid.set({
                columns: [
                        { type: "indexcolumn", header: "序号", width: 35 },
                        {
                            field: "FCount", width: 70, numberFormat: "#,0.00", headerAlign: "center", Align: "right", allowSort: true, header: "满金额",
                            editor: { type: "spinner", minValue: 0, maxValue: 99999999 }
                        },
                        {
                            field: "FDiscount", width: 70, numberFormat: "#,0.00", headerAlign: "center", Align: "right", header: "减金额",
                            editor: { type: "spinner", minValue: 0, maxValue: 99999999 }
                        }
                        /*,{ field: "FRemark", width: 150, headerAlign: "center", header: "备注", editor: { type: "textbox", value: "" } }*/
                ]
            });
        }
        else if (type == 3)
            grid.set({
                columns: [
                    { type: "indexcolumn", header: "序号", width: 35 },
                    { field: "FNumber", width: 120, headerAlign: "center", allowSort: true, header: "商品编码" },
                    { field: "FName", width: 160, headerAlign: "center", allowSort: true, header: "商品名称" },
                    { field: "FSpec", width: 100, headerAlign: "center", allowSort: true, header: "规格型号" },
                    { field: "FUnitName", width: 40, headerAlign: "center", allowSort: true, header: "单位" },
                    {
                        field: "FCount", width: 70, numberFormat: "#,0.00", headerAlign: "center", allowSort: true, header: "满金额", align: "right",
                        editor: { type: "spinner", minValue: 0, maxValue: 99999999 }
                    },
                    {
                        field: "FDiscount", width: 70, numberFormat: "#,0.00", headerAlign: "center", align: "right", header: "返现",
                        editor: { type: "spinner", minValue: 0, maxValue: 99999999 }
                    },
                    { field: "FPrice", width: 70, numberFormat: "#,0.00", headerAlign: "center", align: "right", header: "零售单价" },
                    { field: "FMallPrice", width: 70, numberFormat: "#,0.00", headerAlign: "center", align: "right", header: "商城单价" },
                    { field: "FCode", width: 120, headerAlign: "center", header: "商品条码" }
                    /*,{ field: "FRemark", width: 150, headerAlign: "center", header: "备注", editor: { type: "textbox", value: "" } }*/
                ]
            });
        else if (type == 1 || type == 2) {
            var code = "分类编码";
            var name = "分类名称";
            if (type == 2) {
                code = "品牌编码";
                name = "品牌名称";
            }
            grid.set({
                columns: [
                    { type: "indexcolumn", header: "序号", width: 35 },
                    { field: "FNumber", width: 120, headerAlign: "center", allowSort: true, header: code },
                    { field: "FName", width: 160, headerAlign: "center", allowSort: true, header: name },
                    {
                        field: "FCount", width: 70, numberFormat: "#,0.00", headerAlign: "center", allowSort: true, header: "满金额",
                        editor: { type: "spinner", minValue: 0, maxValue: 99999999 }
                    },
                    {
                        field: "FDiscount", numberFormat: "#,0.00", width: 70, headerAlign: "center", header: "减金额",
                        editor: { type: "spinner", minValue: 0, maxValue: 99999999 }
                    }
                    /*,{ field: "FRemark", width: 150, headerAlign: "center", header: "备注", editor: { type: "textbox", value: "" } }*/
                ]
            });
        }
        grid.clearRows();
    }
    function getGridData(action) {
        var grid = mini.get("datagrid1");
        var row = grid.getSelected();
        for (var i = 0, l = action.length; i < l; i++) {
            var rw = action[i];
            var newRow = {
                FItemID: 0, FID: rw.FItemID, FNumber: rw.FNumber, FName: rw.FName
            };
            grid.addRow(newRow, grid.getData().length);
        }
    }
    function getGoodsClassItem(mSearchKey) {
        mini.open({
            url: "../GetItem/MultIndex",
            title: "商品分类选择", width: 730, height: 460,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", sTbName: "GoodsClsInfo", sSearchKey: "" };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action) {
                    getGridData(action);
                }
            }
        });
    }
    function getBrandItem(mSearchKey) {
        mini.open({
            url: "../GetItem/MultIndex",
            title: "品牌选择", width: 730, height: 460,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", sTbName: "GoodsBrand", sSearchKey: "" };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action) {
                    getGridData(action);
                }
            }
        });
    }

</script>
